function displayshow(id) {
    var show=document.getElementById(id);
    show.style.display="inline-block";
    // return  show;
    // return document.getElementById("id").style.display="inline-block";
}
function displayhidden(id) {
    var hidden=document.getElementById(id);
    hidden.style.display="none";
    // return  hidden;
    // return document.getElementById("id").style.display="none";
}

// 用户
function r_username() {
  var x=document.getElementById("username").value;
  var content=document.getElementById("r_username");
  var reg = /^[\w\u4e00-\u9fa5]{1,6}$/;
  if(x==""){
      content.innerText="用户名不能为空";
      // 根据输入的是否正确，显示正确或错误的图片
      displayshow('wrong01');
      displayhidden('right01');
      content.style.color="red";
      return false;

  }else if(!reg.test(x)){
      content.innerText="请输入1-6位的英文、数字、下划线";
      // 根据输入的是否正确，显示正确或错误的图片
      displayshow('wrong01');
      displayhidden('right01');
      content.style.color="red";
      return false;

  }else if(reg.test(x)){
      content.innerText="用户名正确";
      // 根据输入的是否正确，显示正确或错误的图片
      // document.getElementById("right01").style.display="inline-block";
      // document.getElementById("wrong01").style.display="none";
      displayshow('right01');
      displayhidden('wrong01');
      content.style.color="#0bb40b";
      return true;
  }
  console.log(x);
}

// 手机号
function r_phonenamber() {
    var x=document.getElementById("phonenamber").value;
    var content=document.getElementById("r_phonenamber");
    var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if(x==""){
        content.innerText="手机号不能为空";
        displayshow('wrong02');
        displayhidden('right02');
        content.style.color="red";
        return false;
    }else if(!reg.test(x)){
        content.innerText="请输入正确的手机号";
        displayshow('wrong02');
        displayhidden('right02');
        content.style.color="red";
        return false;
    }else if(reg.test(x)){
        content.innerText="手机号正确";
        displayshow('right02');
        displayhidden('wrong02');
        content.style.color="#0bb40b";
        return false;
    }
}

// 邮箱
function r_email() {
   var x=document.getElementById("email").value;
   var content=document.getElementById("r_email");
   var reg=/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/;
    if(x==""){
        content.innerText="邮箱不能为空";
        displayshow('wrong03');
        displayhidden('right03');
        content.style.color="red";
        return false;
    }else if(!reg.test(x)){
        content.innerText="请输入正确的邮箱";
        displayshow('wrong03');
        displayhidden('right03');
        content.style.color="red";
        return false;
    }else if(reg.test(x)){
        content.innerText="邮箱正确";
        displayshow('right03');
        displayhidden('wrong03');
        content.style.color="#0bb40b";
        return true;
    }
}

<!--输入验证码-->
function r_key() {
    var x=document.getElementById("key").value;
    var content=document.getElementById("r_key");
    if(x=="") {
        content.innerText="验证码不能为空";
        displayshow('wrong04');
        displayhidden('right04');
        content.style.color="red";
        return false;
    }
    if(!x=="") {
        content.innerText="已填入验证码";
        displayshow('wrong04');
        displayhidden('right04');
        content.style.color="#0bb40b";
        return false;
    }
}

<!--输入密码-->
//密码强度正则，最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
    function r_password() {
    var x=document.getElementById("password").value;
    var content=document.getElementById("r_password");
    var reg = /^[\w@#*]{6,12}$/;
    if (x=="") {
        content.innerText="密码不能为空";
        displayshow('wrong05');
        displayhidden('right05');
        content.style.color="red";
        return false;
    }else if(!reg.test(x)){
        content.innerText="密码最少6位,最少含大写字母,小写字母,数字,特殊字符之一";
        displayshow('wrong05');
        displayhidden('right05');
        content.style.color="red";
        return false;
    }else if(reg.test(x)){
        content.innerText="密码正确";
        displayshow('right05');
        displayhidden('wrong05');
        content.style.color="#0bb40b";
        return true;
    }
}

<!--确认密码-->
function r_confirm() {
    var x=document.getElementById("password").value;
    var y=document.getElementById("confirm").value;
    var content=document.getElementById("r_confirm");
    var reg =   /^[\w@#*]{6,12}$/;
    if(reg.test(x)){
        if(y==""){
            content.innerText="请确认密码";
            displayshow('wrong06');
            displayhidden('right06');
            content.style.color="red";
            return false;
        }else if(x==y){
            content.innerText="密码确认正确";
            displayshow('right06');
            displayhidden('wrong06');
            content.style.color="#0bb40b";
            return true;
        }else {
            content.innerText="密码确认错误";
            displayshow('wrong06');
            displayhidden('right06');
            content.style.color="red";
            return false;
        }
    }else {
        content.innerText="请先设置正确的密码";
        displayshow('wrong06');
        displayhidden('right06');
        content.style.color="red";
        return false;
    }
};
function registerForm() {
    if(r_username() && r_phonenamber() && r_email() && r_key() && r_password() && r_confirm()){
         if($("#r-abc").is(':checked')){
             return true;
         }else {
             document.getElementById("r-abc").innerText="接受服务条款才能注册";
         }
    }else {
        return false;
    }
}
//Ajax请求
    $("#regist").click(function(){
        $.ajax({
            url:"http://localhost:8080/user/registered",
            type:"post",
            async:true,
            data:{
                userName:$("#username").val(),
                phone:$("#phonenamber").val(),
                post:$("#email").val(),
                password:$("#password").val(),
                confirm:$("#confirm").val(),
                verificationCode:$("#key").val(),
            },
            success:function(data){
                if(data.code==100){
                    alert("成功");
                    window.location.href='/';
            }else{
                    alert("失败");
                    window.location.href='/registeredjump';
                }
            },
            error:function () {
                alert("异常");
            }
        });
    })

//验证码AJAX
$(".r-form-a").click(function () {
    $.ajax({
        url:"/user/sentPost",
        type:"post",
        data:{
            post:$("#email").val(),
        },
        dataType:"json",
        success:function (data) {
            if(data.code==100){
                alert("成功");
    }else{
                alert("失败");
            }
        }
    });
});
